// pages/confirmorder/confirmorder.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        multiIndex: [],
        currentTab: 0,
        currentBut: 0,
        isHide: true, //支付弹窗是否显示
        typePay: "wx_pay",
        addressId: "", //地址id  不必须传 需在 我的 我的地址列表中获取id
        buttonClicked: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
            }
        })
        that.addressInfo();

    },
    onShow: function() {
        var that = this;
        var listCar = wx.getStorageSync("wholesaleCar");
        var orderCar = [];
        var goodsList = [];
        for (var i in listCar) {
            if (listCar[i].selectStatus == true) {
                var totalSpec = listCar[i].spec_wholesale_price * listCar[i].number;
                listCar[i].totalSpec = totalSpec;
                orderCar.push(listCar[i])
                goodsList.push({
                    goods_id: listCar[i].spec_id,
                    num: listCar[i].number
                });
            }
        }
        that.setData({
            orderList: orderCar,
            goodsOrder: goodsList,
        })
        that.addressInfo();
        that.all(that.data.orderList);
		console.log(that.data.orderList,"lsdkfjkly")
    },
    addressInfo: function() {
        var that = this;
        utils.http("/api/v1/storemanage/store_address", {
            store_id: wx.getStorageSync("userType").storeid
        }, function(res) {
            that.setData({
                defaultAddress: res.datas,
            })
        })
    },
    all: function(listCar) {
        var that = this;
        var totalPrice = 0;
        var price = 0;
        var listCar = that.data.orderList
        for (var i in listCar) {
			price = listCar[i].spec_wholesale_price;
            totalPrice = totalPrice + (parseInt(listCar[i].number) * parseFloat(price));
        }
        that.setData({
            totalPrice: totalPrice.toFixed(2),
            oktotalPrice: totalPrice.toFixed(2)
        })

    },

    //商品支付完成清空对应商品
    shoppClear: function(e) {
        var that = this;
        var listpost = wx.getStorageSync("wholesaleCar");
        var number = wx.getStorageSync("wholesaleNum");
        var inn = 0
        for (var i in listpost) {
            for (var j in that.data.goodsOrder) {
                if (listpost[i].id == that.data.goodsOrder[j].goods_id) {
                    console.log(listpost[i])
                    inn = inn + listpost[i].number
                    listpost.splice(i, 1)
                }
            }
        }
        wx.setStorageSync("number", parseInt(number - inn))
        wx.setStorageSync("wholesaleCar", listpost)
    },
    //立即支付
    onOrderSuccessTap: function(e) {
        var that = this;
        if (that.data.buttonClicked == false) {

            utils.showLoading();
            that.setData({
                buttonClicked: true
            })
            console.log(that.data.defaultAddress.id)
            utils.http("/api/v1/Storemanage/add_order", {
                store_id: wx.getStorageSync("userType").storeid,
                goods_list: that.data.goodsOrder,
                address_id: that.data.defaultAddress.id,
            }, function(res) {
                console.log(res);
                utils.hideLoading();
                if (res.state == 1) {
                    wx.requestPayment({
                        timeStamp: res.datas.timeStamp,
                        nonceStr: res.datas.nonceStr,
                        package: res.datas.package,
                        signType: res.datas.signType,
                        paySign: res.datas.paySign,
                        success(res) {
                            that.shoppClear();
                            wx.redirectTo({
                                url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                            })
                        },
                        fail(res) {
                            that.setData({
                                buttonClicked: false
                            })
                        }
                    })
                } else {
                    utils.message(res.datas);
                }
            })

        }


    },
    //跳入修改地址添加地址页面
    onAddresseditTap: function(e) {
        wx.navigateTo({
            url: '../addressedit/addressedit?id=' + e.currentTarget.dataset.id,
        })
    },
})